home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Parser / tokenizer.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  16KB  |  753 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Tokenizer implementation */
  33.  
  34. #include "pgenheaders.h"
  35.  
  36. #include <ctype.h>
  37.  
  38. #include "tokenizer.h"
  39. #include "errcode.h"
  40.  
  41. extern char *my_readline PROTO((char *));
  42. /* Return malloc'ed string including trailing \n;
  43.    empty malloc'ed string for EOF;
  44.    NULL if interrupted */
  45.  
  46. /* Don't ever change this -- it would break the portability of Python code */
  47. #define TABSIZE 8
  48.  
  49. /* Forward */
  50. static struct tok_state *tok_new PROTO((void));
  51. static int tok_nextc PROTO((struct tok_state *tok));
  52. static void tok_backup PROTO((struct tok_state *tok, int c));
  53.  
  54. /* Token names */
  55.  
  56. char *tok_name[] = {
  57.     "ENDMARKER",
  58.     "NAME",
  59.     "NUMBER",
  60.     "STRING",
  61.     "NEWLINE",
  62.     "INDENT",
  63.     "DEDENT",
  64.     "LPAR",
  65.     "RPAR",
  66.     "LSQB",
  67.     "RSQB",
  68.     "COLON",
  69.     "COMMA",
  70.     "SEMI",
  71.     "PLUS",
  72.     "MINUS",
  73.     "STAR",
  74.     "SLASH",
  75.     "VBAR",
  76.     "AMPER",
  77.     "LESS",
  78.     "GREATER",
  79.     "EQUAL",
  80.     "DOT",
  81.     "PERCENT",
  82.     "BACKQUOTE",
  83.     "LBRACE",
  84.     "RBRACE",
  85.     "EQEQUAL",
  86.     "NOTEQUAL",
  87.     "LESSEQUAL",
  88.     "GREATEREQUAL",
  89.     "TILDE",
  90.     "CIRCUMFLEX",
  91.     "LEFTSHIFT",
  92.     "RIGHTSHIFT",
  93.     "DOUBLESTAR",
  94.     /* This table must match the #defines in token.h! */
  95.     "OP",
  96.     "<ERRORTOKEN>",
  97.     "<N_TOKENS>"
  98. };
  99.  
  100.  
  101. /* Create and initialize a new tok_state structure */
  102.  
  103. static struct tok_state *
  104. tok_new()
  105. {
  106.     struct tok_state *tok = NEW(struct tok_state, 1);
  107.     if (tok == NULL)
  108.         return NULL;
  109.     tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
  110.     tok->done = E_OK;
  111.     tok->fp = NULL;
  112.     tok->tabsize = TABSIZE;
  113.     tok->indent = 0;
  114.     tok->indstack[0] = 0;
  115.     tok->atbol = 1;
  116.     tok->pendin = 0;
  117.     tok->prompt = tok->nextprompt = NULL;
  118.     tok->lineno = 0;
  119.     tok->level = 0;
  120.     return tok;
  121. }
  122.  
  123.  
  124. /* Set up tokenizer for string */
  125.  
  126. struct tok_state *
  127. tok_setups(str)
  128.     char *str;
  129. {
  130.     struct tok_state *tok = tok_new();
  131.     if (tok == NULL)
  132.         return NULL;
  133.     tok->buf = tok->cur = tok->end = tok->inp = str;
  134.     return tok;
  135. }
  136.  
  137.  
  138. /* Set up tokenizer for file */
  139.  
  140. struct tok_state *
  141. tok_setupf(fp, ps1, ps2)
  142.     FILE *fp;
  143.     char *ps1, *ps2;
  144. {
  145.     struct tok_state *tok = tok_new();
  146.     if (tok == NULL)
  147.         return NULL;
  148.     if ((tok->buf = NEW(char, BUFSIZ)) == NULL) {
  149.         DEL(tok);
  150.         return NULL;
  151.     }
  152.     tok->cur = tok->inp = tok->buf;
  153.     tok->end = tok->buf + BUFSIZ;
  154.     tok->fp = fp;
  155.     tok->prompt = ps1;
  156.     tok->nextprompt = ps2;
  157.     return tok;
  158. }
  159.  
  160.  
  161. /* Free a tok_state structure */
  162.  
  163. void
  164. tok_free(tok)
  165.     struct tok_state *tok;
  166. {
  167.     if (tok->fp != NULL && tok->buf != NULL)
  168.         DEL(tok->buf);
  169.     DEL(tok);
  170. }
  171.  
  172.  
  173. /* Get next char, updating state; error code goes into tok->done */
  174.  
  175. static int
  176. tok_nextc(tok)
  177.     register struct tok_state *tok;
  178. {
  179.     for (;;) {
  180.         if (tok->cur != tok->inp) {
  181.             return *tok->cur++; /* Fast path */
  182.         }
  183.         if (tok->done != E_OK)
  184.             return EOF;
  185.         if (tok->fp == NULL) {
  186.             char *end = strchr(tok->inp, '\n');
  187.             if (end != NULL)
  188.                 end++;
  189.             else {
  190.                 end = strchr(tok->inp, '\0');
  191.                 if (end == tok->inp) {
  192.                     tok->done = E_EOF;
  193.                     return EOF;
  194.                 }
  195.             }
  196.             if (tok->start == NULL)
  197.                 tok->buf = tok->cur;
  198.             tok->lineno++;
  199.             tok->inp = end;
  200.             return *tok->cur++;
  201.         }
  202.         if (tok->prompt != NULL) {
  203.             char *new = my_readline(tok->prompt);
  204.             if (tok->nextprompt != NULL)
  205.                 tok->prompt = tok->nextprompt;
  206.             if (new == NULL)
  207.                 tok->done = E_INTR;
  208.             else if (*new == '\0') {
  209.                 free(new);
  210.                 tok->done = E_EOF;
  211.             }
  212.             else if (tok->start != NULL) {
  213.                 int start = tok->start - tok->buf;
  214.                 int oldlen = tok->cur - tok->buf;
  215.                 int newlen = oldlen + strlen(new);
  216.                 char *buf = realloc(tok->buf, newlen+1);
  217.                 tok->lineno++;
  218.                 if (buf == NULL) {
  219.                     free(tok->buf);
  220.                     tok->buf = NULL;
  221.                     free(new);
  222.                     tok->done = E_NOMEM;
  223.                     return EOF;
  224.                 }
  225.                 tok->buf = buf;
  226.                 tok->cur = tok->buf + oldlen;
  227.                 strcpy(tok->buf + oldlen, new);
  228.                 free(new);
  229.                 tok->inp = tok->buf + newlen;
  230.                 tok->end = tok->inp + 1;
  231.                 tok->start = tok->buf + start;
  232.             }
  233.             else {
  234.                 tok->lineno++;
  235.                 if (tok->buf != NULL)
  236.                     free(tok->buf);
  237.                 tok->buf = new;
  238.                 tok->cur = tok->buf;
  239.                 tok->inp = strchr(tok->buf, '\0');
  240.                 tok->end = tok->inp + 1;
  241.             }
  242.         }
  243.         else {
  244.             int done = 0;
  245.             int cur = 0;
  246.             char *pt;
  247.             if (tok->start == NULL) {
  248.                 if (tok->buf == NULL) {
  249.                     tok->buf = NEW(char, BUFSIZ);
  250.                     if (tok->buf == NULL) {
  251.                         tok->done = E_NOMEM;
  252.                         return EOF;
  253.                     }
  254.                     tok->end = tok->buf + BUFSIZ;
  255.                 }
  256.                 if (fgets(tok->buf, (int)(tok->end - tok->buf),
  257.                       tok->fp) == NULL) {
  258.                     tok->done = E_EOF;
  259.                     done = 1;
  260.                 }
  261.                 else {
  262.                     tok->done = E_OK;
  263.                     tok->inp = strchr(tok->buf, '\0');
  264.                     done = tok->inp[-1] == '\n';
  265.                 }
  266.             }
  267.             else {
  268.                 cur = tok->cur - tok->buf;
  269.                 if (feof(tok->fp)) {
  270.                     tok->done = E_EOF;
  271.                     done = 1;
  272.                 }
  273.                 else
  274.                     tok->done = E_OK;
  275.             }
  276.             tok->lineno++;
  277.             /* Read until '\n' or EOF */
  278.             while (!done) {
  279.                 int curstart = tok->start == NULL ? -1 :
  280.                            tok->start - tok->buf;
  281.                 int curvalid = tok->inp - tok->buf;
  282.                 int cursize = tok->end - tok->buf;
  283.                 int newsize = curvalid + BUFSIZ;
  284.                 char *newbuf = tok->buf;
  285.                 RESIZE(newbuf, char, newsize);
  286.                 if (newbuf == NULL) {
  287.                     tok->done = E_NOMEM;
  288.                     tok->cur = tok->inp;
  289.                     return EOF;
  290.                 }
  291.                 tok->buf = newbuf;
  292.                 tok->inp = tok->buf + curvalid;
  293.                 tok->end = tok->buf + newsize;
  294.                 tok->start = curstart < 0 ? NULL :
  295.                          tok->buf + curstart;
  296.                 if (fgets(tok->inp,
  297.                            (int)(tok->end - tok->inp),
  298.                            tok->fp) == NULL) {
  299.                     /* Last line does not end in \n,
  300.                        fake one */
  301.                     strcpy(tok->inp, "\n");
  302.                 }
  303.                 tok->inp = strchr(tok->inp, '\0');
  304.                 done = tok->inp[-1] == '\n';
  305.             }
  306.             tok->cur = tok->buf + cur;
  307.             /* replace "\r\n" with "\n" */
  308.             pt = tok->inp - 2;
  309.             if (pt >= tok->buf && *pt == '\r') {
  310.                 *pt++ = '\n';
  311.                 *pt = '\0';
  312.                 tok->inp = pt;
  313.             }
  314.         }
  315.         if (tok->done != E_OK) {
  316.             if (tok->prompt != NULL)
  317.                 fprintf(stderr, "\n");
  318.             tok->cur = tok->inp;
  319.             return EOF;
  320.         }
  321.     }
  322.     /*NOTREACHED*/
  323. }
  324.  
  325.  
  326. /* Back-up one character */
  327.  
  328. static void
  329. tok_backup(tok, c)
  330.     register struct tok_state *tok;
  331.     register int c;
  332. {
  333.     if (c != EOF) {
  334.         if (--tok->cur < tok->buf)
  335.             fatal("tok_backup: begin of buffer");
  336.         if (*tok->cur != c)
  337.             *tok->cur = c;
  338.     }
  339. }
  340.  
  341.  
  342. /* Return the token corresponding to a single character */
  343.  
  344. int
  345. tok_1char(c)
  346.     int c;
  347. {
  348.     switch (c) {
  349.     case '(':    return LPAR;
  350.     case ')':    return RPAR;
  351.     case '[':    return LSQB;
  352.     case ']':    return RSQB;
  353.     case ':':    return COLON;
  354.     case ',':    return COMMA;
  355.     case ';':    return SEMI;
  356.     case '+':    return PLUS;
  357.     case '-':    return MINUS;
  358.     case '*':    return STAR;
  359.     case '/':    return SLASH;
  360.     case '|':    return VBAR;
  361.     case '&':    return AMPER;
  362.     case '<':    return LESS;
  363.     case '>':    return GREATER;
  364.     case '=':    return EQUAL;
  365.     case '.':    return DOT;
  366.     case '%':    return PERCENT;
  367.     case '`':    return BACKQUOTE;
  368.     case '{':    return LBRACE;
  369.     case '}':    return RBRACE;
  370.     case '^':    return CIRCUMFLEX;
  371.     case '~':    return TILDE;
  372.     default:    return OP;
  373.     }
  374. }
  375.  
  376.  
  377. int
  378. tok_2char(c1, c2)
  379.     int c1, c2;
  380. {
  381.     switch (c1) {
  382.     case '=':
  383.         switch (c2) {
  384.         case '=':    return EQEQUAL;
  385.         }
  386.         break;
  387.     case '!':
  388.         switch (c2) {
  389.         case '=':    return NOTEQUAL;
  390.         }
  391.         break;
  392.     case '<':
  393.         switch (c2) {
  394.         case '>':    return NOTEQUAL;
  395.         case '=':    return LESSEQUAL;
  396.         case '<':    return LEFTSHIFT;
  397.         }
  398.         break;
  399.     case '>':
  400.         switch (c2) {
  401.         case '=':    return GREATEREQUAL;
  402.         case '>':    return RIGHTSHIFT;
  403.         }
  404.         break;
  405.     case '*':
  406.         switch (c2) {
  407.         case '*':    return DOUBLESTAR;
  408.         }
  409.         break;
  410.     }
  411.     return OP;
  412. }
  413.  
  414.  
  415. /* Get next token, after space stripping etc. */
  416.  
  417. int
  418. tok_get(tok, p_start, p_end)
  419.     register struct tok_state *tok; /* In/out: tokenizer state */
  420.     char **p_start, **p_end; /* Out: point to start/end of token */
  421. {
  422.     register int c;
  423.     int blankline;
  424.  
  425.     *p_start = *p_end = NULL;
  426.   nextline:
  427.     tok->start = NULL;
  428.     blankline = 0;
  429.  
  430.     /* Get indentation level */
  431.     if (tok->atbol) {
  432.         register int col = 0;
  433.         tok->atbol = 0;
  434.         for (;;) {
  435.             c = tok_nextc(tok);
  436.             if (c == ' ')
  437.                 col++;
  438.             else if (c == '\t')
  439.                 col = (col/tok->tabsize + 1) * tok->tabsize;
  440.             else if (c == '\014') /* Control-L (formfeed) */
  441.                 col = 0; /* For Emacs users */
  442.             else
  443.                 break;
  444.         }
  445.         tok_backup(tok, c);
  446.         if (c == '#' || c == '\n') {
  447.             /* Lines with only whitespace and/or comments
  448.                shouldn't affect the indentation and are
  449.                not passed to the parser as NEWLINE tokens,
  450.                except *totally* empty lines in interactive
  451.                mode, which signal the end of a command group. */
  452.             if (col == 0 && c == '\n' && tok->prompt != NULL)
  453.                 blankline = 0; /* Let it through */
  454.             else
  455.                 blankline = 1; /* Ignore completely */
  456.             /* We can't jump back right here since we still
  457.                may need to skip to the end of a comment */
  458.         }
  459.         if (!blankline && tok->level == 0) {
  460.             if (col == tok->indstack[tok->indent]) {
  461.                 /* No change */
  462.             }
  463.             else if (col > tok->indstack[tok->indent]) {
  464.                 /* Indent -- always one */
  465.                 if (tok->indent+1 >= MAXINDENT) {
  466.                     fprintf(stderr, "excessive indent\n");
  467.                     tok->done = E_TOKEN;
  468.                     tok->cur = tok->inp;
  469.                     return ERRORTOKEN;
  470.                 }
  471.                 tok->pendin++;
  472.                 tok->indstack[++tok->indent] = col;
  473.             }
  474.             else /* col < tok->indstack[tok->indent] */ {
  475.                 /* Dedent -- any number, must be consistent */
  476.                 while (tok->indent > 0 &&
  477.                     col < tok->indstack[tok->indent]) {
  478.                     tok->indent--;
  479.                     tok->pendin--;
  480.                 }
  481.                 if (col != tok->indstack[tok->indent]) {
  482.                     fprintf(stderr, "inconsistent dedent\n");
  483.                     tok->done = E_TOKEN;
  484.                     tok->cur = tok->inp;
  485.                     return ERRORTOKEN;
  486.                 }
  487.             }
  488.         }
  489.     }
  490.     
  491.     tok->start = tok->cur;
  492.     
  493.     /* Return pending indents/dedents */
  494.     if (tok->pendin != 0) {
  495.         if (tok->pendin < 0) {
  496.             tok->pendin++;
  497.             return DEDENT;
  498.         }
  499.         else {
  500.             tok->pendin--;
  501.             return INDENT;
  502.         }
  503.     }
  504.     
  505.  again:
  506.     tok->start = NULL;
  507.     /* Skip spaces */
  508.     do {
  509.         c = tok_nextc(tok);
  510.     } while (c == ' ' || c == '\t' || c == '\014');
  511.     
  512.     /* Set start of current token */
  513.     tok->start = tok->cur - 1;
  514.     
  515.     /* Skip comment */
  516.     if (c == '#') {
  517.         /* Hack to allow overriding the tabsize in the file.
  518.            This is also recognized by vi, when it occurs near the
  519.            beginning or end of the file.  (Will vi never die...?)
  520.            For Python it must be at the beginning of the file! */
  521.         /* XXX The real vi syntax is actually different :-( */
  522.         /* XXX Should recognize Emacs syntax, too */
  523.         int x;
  524.         if (sscanf(tok->cur,
  525.                 " vi:set tabsize=%d:", &x) == 1 &&
  526.                         x >= 1 && x <= 40) {
  527.             /* fprintf(stderr, "# vi:set tabsize=%d:\n", x); */
  528.             tok->tabsize = x;
  529.         }
  530.         do {
  531.             c = tok_nextc(tok);
  532.         } while (c != EOF && c != '\n');
  533.     }
  534.     
  535.     /* Check for EOF and errors now */
  536.     if (c == EOF) {
  537.         return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
  538.     }
  539.     
  540.     /* Identifier (most frequent token!) */
  541.     if (isalpha(c) || c == '_') {
  542.         do {
  543.             c = tok_nextc(tok);
  544.         } while (isalnum(c) || c == '_');
  545.         tok_backup(tok, c);
  546.         *p_start = tok->start;
  547.         *p_end = tok->cur;
  548.         return NAME;
  549.     }
  550.     
  551.     /* Newline */
  552.     if (c == '\n') {
  553.         tok->atbol = 1;
  554.         if (blankline || tok->level > 0)
  555.             goto nextline;
  556.         *p_start = tok->start;
  557.         *p_end = tok->cur - 1; /* Leave '\n' out of the string */
  558.         return NEWLINE;
  559.     }
  560.     
  561.     /* Period or number starting with period? */
  562.     if (c == '.') {
  563.         c = tok_nextc(tok);
  564.         if (isdigit(c)) {
  565.             goto fraction;
  566.         }
  567.         else {
  568.             tok_backup(tok, c);
  569.             *p_start = tok->start;
  570.             *p_end = tok->cur;
  571.             return DOT;
  572.         }
  573.     }
  574.  
  575.     /* Number */
  576.     if (isdigit(c)) {
  577.         if (c == '0') {
  578.             /* Hex or octal */
  579.             c = tok_nextc(tok);
  580.             if (c == '.')
  581.                 goto fraction;
  582. #ifndef WITHOUT_COMPLEX
  583.             if (c == 'j' || c == 'J')
  584.                 goto imaginary;
  585. #endif
  586.             if (c == 'x' || c == 'X') {
  587.                 /* Hex */
  588.                 do {
  589.                     c = tok_nextc(tok);
  590.                 } while (isxdigit(c));
  591.             }
  592.             else {
  593.                 /* XXX This is broken!  E.g.,
  594.                    09.9 should be accepted as float! */
  595.                 /* Octal; c is first char of it */
  596.                 /* There's no 'isoctdigit' macro, sigh */
  597.                 while ('0' <= c && c < '8') {
  598.                     c = tok_nextc(tok);
  599.                 }
  600.             }
  601.             if (c == 'l' || c == 'L')
  602.                 c = tok_nextc(tok);
  603.         }
  604.         else {
  605.             /* Decimal */
  606.             do {
  607.                 c = tok_nextc(tok);
  608.             } while (isdigit(c));
  609.             if (c == 'l' || c == 'L')
  610.                 c = tok_nextc(tok);
  611.             else {
  612.                 /* Accept floating point numbers.
  613.                    XXX This accepts incomplete things like
  614.                    XXX 12e or 1e+; worry run-time */
  615.                 if (c == '.') {
  616.         fraction:
  617.                     /* Fraction */
  618.                     do {
  619.                         c = tok_nextc(tok);
  620.                     } while (isdigit(c));
  621.                 }
  622.                 if (c == 'e' || c == 'E') {
  623.                     /* Exponent part */
  624.                     c = tok_nextc(tok);
  625.                     if (c == '+' || c == '-')
  626.                         c = tok_nextc(tok);
  627.                     while (isdigit(c)) {
  628.                         c = tok_nextc(tok);
  629.                     }
  630.                 }
  631. #ifndef WITHOUT_COMPLEX
  632.                 if (c == 'j' || c == 'J')
  633.                     /* Imaginary part */
  634.         imaginary:
  635.                     c = tok_nextc(tok);
  636. #endif
  637.             }
  638.         }
  639.         tok_backup(tok, c);
  640.         *p_start = tok->start;
  641.         *p_end = tok->cur;
  642.         return NUMBER;
  643.     }
  644.     
  645.     /* String */
  646.     if (c == '\'' || c == '"') {
  647.         int quote = c;
  648.         int triple = 0;
  649.         int tripcount = 0;
  650.         for (;;) {
  651.             c = tok_nextc(tok);
  652.             if (c == '\n') {
  653.                 if (!triple) {
  654.                     tok->done = E_TOKEN;
  655.                     tok_backup(tok, c);
  656.                     return ERRORTOKEN;
  657.                 }
  658.                 tripcount = 0;
  659.             }
  660.             else if (c == EOF) {
  661.                 tok->done = E_TOKEN;
  662.                 tok->cur = tok->inp;
  663.                 return ERRORTOKEN;
  664.             }
  665.             else if (c == quote) {
  666.                 tripcount++;
  667.                 if (tok->cur == tok->start+2) {
  668.                     c = tok_nextc(tok);
  669.                     if (c == quote) {
  670.                         triple = 1;
  671.                         tripcount = 0;
  672.                         continue;
  673.                     }
  674.                     tok_backup(tok, c);
  675.                 }
  676.                 if (!triple || tripcount == 3)
  677.                     break;
  678.             }
  679.             else if (c == '\\') {
  680.                 tripcount = 0;
  681.                 c = tok_nextc(tok);
  682.                 if (c == EOF) {
  683.                     tok->done = E_TOKEN;
  684.                     tok->cur = tok->inp;
  685.                     return ERRORTOKEN;
  686.                 }
  687.             }
  688.             else
  689.                 tripcount = 0;
  690.         }
  691.         *p_start = tok->start;
  692.         *p_end = tok->cur;
  693.         return STRING;
  694.     }
  695.     
  696.     /* Line continuation */
  697.     if (c == '\\') {
  698.         c = tok_nextc(tok);
  699.         if (c != '\n') {
  700.             tok->done = E_TOKEN;
  701.             tok->cur = tok->inp;
  702.             return ERRORTOKEN;
  703.         }
  704.         goto again; /* Read next line */
  705.     }
  706.     
  707.     /* Check for two-character token */
  708.     {
  709.         int c2 = tok_nextc(tok);
  710.         int token = tok_2char(c, c2);
  711.         if (token != OP) {
  712.             *p_start = tok->start;
  713.             *p_end = tok->cur;
  714.             return token;
  715.         }
  716.         tok_backup(tok, c2);
  717.     }
  718.     
  719.     /* Keep track of parentheses nesting level */
  720.     switch (c) {
  721.     case '(':
  722.     case '[':
  723.     case '{':
  724.         tok->level++;
  725.         break;
  726.     case ')':
  727.     case ']':
  728.     case '}':
  729.         tok->level--;
  730.         break;
  731.     }
  732.     
  733.     /* Punctuation character */
  734.     *p_start = tok->start;
  735.     *p_end = tok->cur;
  736.     return tok_1char(c);
  737. }
  738.  
  739.  
  740. #ifdef DEBUG
  741.  
  742. void
  743. tok_dump(type, start, end)
  744.     int type;
  745.     char *start, *end;
  746. {
  747.     printf("%s", tok_name[type]);
  748.     if (type == NAME || type == NUMBER || type == STRING || type == OP)
  749.         printf("(%.*s)", (int)(end - start), start);
  750. }
  751.  
  752. #endif
  753.